package com.gz.furnitures.web.throwedServlet;

import com.gz.furnitures.entity.Member;
import com.gz.furnitures.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    //定义一个属性 MemberService
    private MemberServiceImpl memberService = new MemberServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("doPost被调用");
        //接收用户注册信息->到前端页面查看字段
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        //判断用户名是否可用
        if(!memberService.isExistsUsername(username)){
//            System.out.println("用户名 "+username+" 不存在，可注册");
            Member member = new Member(null,username, password, email);
            if(memberService.registerMember(member)){
                System.out.println("注册成功");
            }else {
                System.out.println("注册error");
            }
        }else{
            System.out.println("用户名"+username+"已存在，不可注册");
            //返回注册页面
        }
    }
}
